package com.pb.b2b.bean;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

import com.pb.b2b.model.Category;
import com.pb.b2b.model.CategoryLanguage;
import com.pb.b2b.model.enums.Status;
import com.pb.b2b.util.FacesUtils;

public abstract class CategoryBaseBean extends BaseBean {

	public TreeNode root;
	public TreeNode selectedNode;

	public boolean showRootCategory;
	public List<CategoryLanguage> defaultCategoryLanguages;
	public CategoryLanguage selectedCategoryLanguage;

	public void treeInit() {
		Map<Long, TreeNode> treeMap = new HashedMap();
		root = new DefaultTreeNode("Root", null);
		TreeNode anaKategori = null;
		if (showRootCategory) {
			CategoryLanguage rootCategoryLanguage = new CategoryLanguage();
			rootCategoryLanguage.setName("ROOT");
			rootCategoryLanguage.setDescription("Ana Kategori");
			Category rootCategory = new Category();
			rootCategory.setRowNumber(0);
			rootCategory.setStatus(Status.AKTIVE);
			rootCategoryLanguage.setCategory(rootCategory);
			anaKategori = new DefaultTreeNode(rootCategoryLanguage, root);
			anaKategori.setExpanded(true);
		}

		for (int index = 0; index < defaultCategoryLanguages.size(); index++) {
			CategoryLanguage categoryLanguage = defaultCategoryLanguages.get(index);
			if (categoryLanguage.getCategory().getParentCategory() == null) {
				treeMap.put(categoryLanguage.getCategory().getId(), new DefaultTreeNode(categoryLanguage,
						showRootCategory ? anaKategori : root));
			} else {
				if (treeMap.get(categoryLanguage.getCategory().getParentCategory().getId()) != null) {
					treeMap.put(
							categoryLanguage.getCategory().getId(),
							new DefaultTreeNode(categoryLanguage,
									treeMap.get(categoryLanguage.getCategory().getParentCategory().getId())));
					treeMap.get(categoryLanguage.getCategory().getId()).setSelectable(
							treeMap.get(categoryLanguage.getCategory().getId()).getParent().isSelectable());
				} else {
					defaultCategoryLanguages.add(categoryLanguage);
				}
			}
			if (treeMap.get(categoryLanguage.getCategory().getId()) != null) {
				treeMap.get(categoryLanguage.getCategory().getId()).setExpanded(true);
			}
			if (showRootCategory && selectedCategoryLanguage != null
					&& categoryLanguage.getId().equals(selectedCategoryLanguage.getId())) {
				if (treeMap.get(categoryLanguage.getCategory().getId()) != null) {
					treeMap.get(categoryLanguage.getCategory().getId()).getParent().setSelected(true);
					treeMap.get(categoryLanguage.getCategory().getId()).setSelectable(false);
				}
			}

		}
	}

	public String getStatusText(boolean value) {
		String bundleName = "resources.application";
		String tmp = FacesUtils.getResourceBundleString(bundleName, "lbl_status_" + (value ? "1" : "0"), null);
		return tmp;
	}

	/* GETTER _ SETTER */
	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getSelectedNode() {
		return selectedNode;
	}

	public void setSelectedNode(TreeNode selectedNode) {
		this.selectedNode = selectedNode;
	}

	public List<CategoryLanguage> getDefaultCategoryLanguages() {
		return defaultCategoryLanguages;
	}

	public void setDefaultCategoryLanguages(List<CategoryLanguage> defaultCategoryLanguages) {
		this.defaultCategoryLanguages = defaultCategoryLanguages;
	}

}
